package com.company.project.controller.ldjsc.menu;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.company.project.entity.ldjsc.menu.ZyMenuEntity;
import com.company.project.service.ldjsc.menu.ZyMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import com.company.project.common.utils.DataResult;





/**
 * zy菜单表
 *
 * @author wenbin
 * @email *****@mail.com
 * @date 2025-07-08 11:11:05
 */
@Controller
@RequestMapping("/sxzy/mune")
public class ZyMenuController {
    @Autowired
    private ZyMenuService zyMenuService;

    /**
    * 跳转到页面
    */
    @GetMapping("/index/zyMenu")
    public String zyMenu() {
        return "zymenu/list";
    }


    @ApiOperation(value = "查询分页数据")
    @PostMapping("/listByPage")
    @SaCheckPermission("zyMenu:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody ZyMenuEntity zyMenu){
        LambdaQueryWrapper<ZyMenuEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        queryWrapper.eq(zyMenu.getId() != null, ZyMenuEntity::getId, zyMenu.getId());
        queryWrapper.orderByDesc(ZyMenuEntity::getId);
        IPage<ZyMenuEntity> iPage = zyMenuService.page(zyMenu.getQueryPage(), queryWrapper);
        return DataResult.success(iPage);
    }


    @ApiOperation(value = "新增")
    @PostMapping("/add")
    @SaCheckPermission("zyMenu:add")
    @ResponseBody
    public DataResult add(@RequestBody ZyMenuEntity zyMenu){
            zyMenuService.save(zyMenu);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/delete")
    @SaCheckPermission("zyMenu:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
            zyMenuService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("/update")
    @SaCheckPermission("zyMenu:update")
    @ResponseBody
    public DataResult update(@RequestBody ZyMenuEntity zyMenu){
            zyMenuService.updateById(zyMenu);
        return DataResult.success();
    }

    @ApiOperation(value = "查询当前菜单信息")
    @GetMapping("/query")
//    @SaCheckPermission("zyMenu:query")
    @ResponseBody
    public DataResult query(String name, String parentName, String grade) {

        if (name == null || parentName == null || grade == null){
            return DataResult.fail("参数错误！");
        }

        LambdaQueryWrapper<ZyMenuEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ZyMenuEntity::getName, name);
        queryWrapper.eq(ZyMenuEntity::getParentName, parentName);
        queryWrapper.eq(ZyMenuEntity::getGrade, grade);
        queryWrapper.last(" limit 1");
        ZyMenuEntity one = zyMenuService.getOne(queryWrapper);
        return DataResult.success(one);
    }

}
